<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>line.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_line.js.html">line.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Represents a line in 2D space.
<a name="line18"></a> *
<a name="line19"></a> * @author robbyw@google.com (Robby Walker)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.math.Line&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.math&#39;);
<a name="line25"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Object representing a line.
<a name="line31"></a> * @param {number} x0 X coordinate of the start point.
<a name="line32"></a> * @param {number} y0 Y coordinate of the start point.
<a name="line33"></a> * @param {number} x1 X coordinate of the end point.
<a name="line34"></a> * @param {number} y1 Y coordinate of the end point.
<a name="line35"></a> * @constructor
<a name="line36"></a> */
<a name="line37"></a>goog.math.Line = function(x0, y0, x1, y1) {
<a name="line38"></a>  /**
<a name="line39"></a>   * X coordinate of the first point.
<a name="line40"></a>   * @type {number}
<a name="line41"></a>   */
<a name="line42"></a>  this.x0 = x0;
<a name="line43"></a>
<a name="line44"></a>  /**
<a name="line45"></a>   * Y coordinate of the first point.
<a name="line46"></a>   * @type {number}
<a name="line47"></a>   */
<a name="line48"></a>  this.y0 = y0;
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * X coordinate of the first control point.
<a name="line52"></a>   * @type {number}
<a name="line53"></a>   */
<a name="line54"></a>  this.x1 = x1;
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * Y coordinate of the first control point.
<a name="line58"></a>   * @type {number}
<a name="line59"></a>   */
<a name="line60"></a>  this.y1 = y1;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * @return {!goog.math.Line} A copy of this line.
<a name="line66"></a> */
<a name="line67"></a>goog.math.Line.prototype.clone = function() {
<a name="line68"></a>  return new goog.math.Line(this.x0, this.y0, this.x1, this.y1);
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Tests whether the given line is exactly the same as this one.
<a name="line74"></a> * @param {goog.math.Line} other The other line.
<a name="line75"></a> * @return {boolean} Whether the given line is the same as this one.
<a name="line76"></a> */
<a name="line77"></a>goog.math.Line.prototype.equals = function(other) {
<a name="line78"></a>  return this.x0 == other.x0 &amp;&amp; this.y0 == other.y0 &amp;&amp;
<a name="line79"></a>         this.x1 == other.x1 &amp;&amp; this.y1 == other.y1;
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * @return {number} The squared length of the line segment used to define the
<a name="line85"></a> *     line.
<a name="line86"></a> */
<a name="line87"></a>goog.math.Line.prototype.getSegmentLengthSquared = function() {
<a name="line88"></a>  var xdist = this.x1 - this.x0;
<a name="line89"></a>  var ydist = this.y1 - this.y0;
<a name="line90"></a>  return xdist * xdist + ydist * ydist;
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * @return {number} The length of the line segment used to define the line.
<a name="line96"></a> */
<a name="line97"></a>goog.math.Line.prototype.getSegmentLength = function() {
<a name="line98"></a>  return Math.sqrt(this.getSegmentLengthSquared());
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Computes the interpolation parameter for the point on the line closest to
<a name="line104"></a> * a given point.
<a name="line105"></a> * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
<a name="line106"></a> *     a coordinate object.
<a name="line107"></a> * @param {number=} opt_y The y coordinate of the point - required if x is a
<a name="line108"></a> *     number, ignored if x is a goog.math.Coordinate.
<a name="line109"></a> * @return {number} The interpolation parameter of the point on the line
<a name="line110"></a> *     closest to the given point.
<a name="line111"></a> * @private
<a name="line112"></a> */
<a name="line113"></a>goog.math.Line.prototype.getClosestLinearInterpolation_ = function(x, opt_y) {
<a name="line114"></a>  var y;
<a name="line115"></a>  if (x instanceof goog.math.Coordinate) {
<a name="line116"></a>    y = x.y;
<a name="line117"></a>    x = x.x;
<a name="line118"></a>  } else {
<a name="line119"></a>    y = opt_y;
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  var x0 = this.x0;
<a name="line123"></a>  var y0 = this.y0;
<a name="line124"></a>
<a name="line125"></a>  var xChange = this.x1 - x0;
<a name="line126"></a>  var yChange = this.y1 - y0;
<a name="line127"></a>
<a name="line128"></a>  return ((x - x0) * xChange + (y - y0) * yChange) /
<a name="line129"></a>      this.getSegmentLengthSquared();
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Returns the point on the line segment proportional to t, where for t = 0 we
<a name="line135"></a> * return the starting point and for t = 1 we return the end point.  For t &lt; 0
<a name="line136"></a> * or t &gt; 1 we extrapolate along the line defined by the line segment.
<a name="line137"></a> * @param {number} t The interpolation parameter along the line segment.
<a name="line138"></a> * @return {!goog.math.Coordinate} The point on the line segment at t.
<a name="line139"></a> */
<a name="line140"></a>goog.math.Line.prototype.getInterpolatedPoint = function(t) {
<a name="line141"></a>  return new goog.math.Coordinate(
<a name="line142"></a>      goog.math.lerp(this.x0, this.x1, t),
<a name="line143"></a>      goog.math.lerp(this.y0, this.y1, t));
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Computes the point on the line closest to a given point.  Note that a line
<a name="line149"></a> * in this case is defined as the infinite line going through the start and end
<a name="line150"></a> * points.  To find the closest point on the line segment itself see
<a name="line151"></a> * {@see #getClosestSegmentPoint}.
<a name="line152"></a> * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
<a name="line153"></a> *     a coordinate object.
<a name="line154"></a> * @param {number=} opt_y The y coordinate of the point - required if x is a
<a name="line155"></a> *     number, ignored if x is a goog.math.Coordinate.
<a name="line156"></a> * @return {!goog.math.Coordinate} The point on the line closest to the given
<a name="line157"></a> *     point.
<a name="line158"></a> */
<a name="line159"></a>goog.math.Line.prototype.getClosestPoint = function(x, opt_y) {
<a name="line160"></a>  return this.getInterpolatedPoint(
<a name="line161"></a>      this.getClosestLinearInterpolation_(x, opt_y));
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Computes the point on the line segment closest to a given point.
<a name="line167"></a> * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
<a name="line168"></a> *     a coordinate object.
<a name="line169"></a> * @param {number=} opt_y The y coordinate of the point - required if x is a
<a name="line170"></a> *     number, ignored if x is a goog.math.Coordinate.
<a name="line171"></a> * @return {!goog.math.Coordinate} The point on the line segment closest to the
<a name="line172"></a> *     given point.
<a name="line173"></a> */
<a name="line174"></a>goog.math.Line.prototype.getClosestSegmentPoint = function(x, opt_y) {
<a name="line175"></a>  return this.getInterpolatedPoint(
<a name="line176"></a>      goog.math.clamp(this.getClosestLinearInterpolation_(x, opt_y), 0, 1));
<a name="line177"></a>};
</pre>


</body>
</html>
